/*
 * Copyright (c) 2019-2020, https://api.iot.10086.cn/(https://gitee.com/cmcc-iot-api/iot-card-open-application/).
 */
package cn.iot.card.open.sim.service;

import cn.iot.card.open.constants.Constants;
import cn.iot.card.open.constants.ErrorCode;
import cn.iot.card.open.constants.UrlConstants;
import cn.iot.card.open.ec.dto.ResponseDto;
import cn.iot.card.open.modular.model.ResultDTO;
import cn.iot.card.open.modular.token.TokenService;
import cn.iot.card.open.pojo.page.LayuiPageFactory;
import cn.iot.card.open.pojo.page.LayuiPageInfo;
import cn.iot.card.open.sim.dto.general.BusinessJobIdDTO;
import cn.iot.card.open.sim.dto.general.SimServiceTypeDTO;
import cn.iot.card.open.sim.dto.general.SimServiceTypeListDTO;
import cn.iot.card.open.sim.warpper.GeneralCommunicateWrapper;
import cn.iot.card.open.util.ApiRequestUtil;
import cn.iot.card.open.util.CommonUtil;
import cn.iot.card.open.util.JsonUtil;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物联卡综合信息-通信服务实现类
 *
 * @author 何光鑫
 * @date 20200325 15:28:29
 * @modify 20200325 何光鑫 v1.1.2.1 创建
 * @since v1.1.2.1
 */
@Service
@Slf4j
public class GeneralCommunicateService {

    /**
     * apiRequestUtil
     */
    @Autowired
    private ApiRequestUtil apiRequestUtil;

    /**
     * tokenService
     */
    @Autowired
    private TokenService tokenService;

    /**
     * 查询通信功能开通状态
     *
     * @param transId
     * @param accountId
     * @param parameterMap
     * @return 返回 layui page info
     * @throws IOException
     * @author 何光鑫
     * @date 20200325 15:28:29
     * @modify 20200325 何光鑫 v1.1.2.1 创建
     * @since v1.1.2.1
     */
    public LayuiPageInfo queryCommunicateFunctionStatus(String transId, String accountId , Map<String, String> parameterMap) throws IOException {
        Page page = LayuiPageFactory.defaultPage();
        String url = UrlConstants.SIM_COMMUNICATION_FUNCTION_STATUS;
        // 调用api接口：先获取token，然后带着token调用api接口
        ResultDTO tokenResultDTO = doRequest(true, accountId, transId,parameterMap,url);
        // 如果此处响应失败即为获取token失败，返回失败原因
        if (!tokenResultDTO.getCode().equals(ErrorCode.SUCCESS.getCode())) {
            ErrorCode errorCode = ErrorCode.getByCode(tokenResultDTO.getCode());
            log.warn("[{}] query communicateFunctionStatus token error first,error message is {}.",transId, errorCode.getMsg());
            return LayuiPageFactory.cteateErrorMsg(errorCode);
        }
        // 获取token成功后解析api接口的响应结果
        String responseStr = tokenResultDTO.getMsg();
        String code = CommonUtil.getApiRspCode(responseStr);
        // 获取api接口的响应结果状态码，如果状态码为12021（token不存在或已过期），则重新获取token后再次调用api接口
        if (ErrorCode.ERR12021.getCode().equals(code)) {
            log.warn("[{}] token[accountId:{}] of query communicateFunctionStatus is invalid and start to get a new token.",transId, accountId);
            tokenResultDTO = doRequest(false, accountId, transId, parameterMap, url);
            // 如果此处响应失败即为获取token失败，返回失败原因
            if (!tokenResultDTO.getCode().equals(ErrorCode.SUCCESS.getCode())) {
                ErrorCode errorCode = ErrorCode.getByCode(tokenResultDTO.getCode());
                log.warn("[{}] query communicateFunctionStatus token error second,error message is {}.",transId, errorCode.getMsg());
                return LayuiPageFactory.cteateErrorMsg(errorCode);
            }
            // 获取token成功后，重置responseStr（api接口的响应结果）
            responseStr = tokenResultDTO.getMsg();
        }
        log.info("[{}] query communicateFunctionStatus result string is {}.",transId, responseStr);
        // 组装页面信息
        ResponseDto<SimServiceTypeListDTO> result = JsonUtil.parseJson(responseStr, new TypeReference<ResponseDto<SimServiceTypeListDTO>>() {});
        code = result.getStatus();
        if (!ErrorCode.SUCCESS.getCode().equals(code)) {
            // 如果api接口返回失败，则返回失败原因
            log.warn("[{}] query communicateFunctionStatus error,error message is {}.",transId, result.getMessage());
            return LayuiPageFactory.cteateErrorMsg(ErrorCode.getByCode(code));
        }
        List<SimServiceTypeListDTO> simServiceTypeListDTOS = result.getResult();
        if (ToolUtil.isEmpty(simServiceTypeListDTOS)) {
            log.warn("[{}] query communicateFunctionStatus end,result is null.",transId);
            return LayuiPageFactory.createEmptyPageInfo();
        }
        List<SimServiceTypeDTO> serviceTypeList = simServiceTypeListDTOS.get(0).getServiceTypeList();

        //封装参数
        List<Map<String,Object>> resultMaps = new ArrayList<>();
        if(ToolUtil.isNotEmpty(serviceTypeList)){
            for(SimServiceTypeDTO simServiceTypeDTO : serviceTypeList){
                Map<String,Object> map = new HashMap<>();
                map.put(Constants.RESPONSE_SERVICE_TYPE,simServiceTypeDTO.getServiceType());
                map.put(Constants.RESPONSE_SERVICE_STATUS,simServiceTypeDTO.getServiceStatus());
                map.put(Constants.REQUEST_PARAM_APNNAME,simServiceTypeDTO.getApnName());
                resultMaps.add(map);
            }
        }
        page.setRecords(resultMaps);
        Page wrapped = new GeneralCommunicateWrapper(page).wrap();
        return LayuiPageFactory.createPageInfo(wrapped);
    }

    /**
     * 基础通信功能开停批量办理
     *
     * @param transId
     * @param accountId
     * @param msisdns
     * @param serviceType
     * @param operType
     * @param apnName
     * @return 返回 response data
     * @throws IOException
     * @author 何光鑫
     * @date 20200325 15:41:48
     * @modify 20200325 何光鑫 v1.1.2.1 创建
     * @since v1.1.2.1
     */
    public ResponseData simCommunicationFunctionBatch(String transId, String accountId, String msisdns, String serviceType, String operType, String apnName) throws IOException {
        ResponseData responseData = new ResponseData();
        // 封装调用api接口的参数
        Map<String, String> parameterMap = new HashMap<>();
        String url = UrlConstants.SIM_COMMUNICATION_FUNCTION_BATCH;
        parameterMap.put(Constants.REQUEST_PARAM_MSISDNS,msisdns);
        parameterMap.put(Constants.RESPONSE_SERVICE_TYPE,serviceType);
        parameterMap.put(Constants.REQUEST_PARAM_OPERTYPE,operType);
        //当服务类型为11时，apn名称必填
        if(serviceType.equals(Constants.RESPONSE_SERVICE_TYPE_11)) {
            parameterMap.put(Constants.REQUEST_PARAM_APNNAME, apnName);
        }
        // 调用api接口：先获取token，然后带着token调用api接口
        ResultDTO tokenResultDTO = doRequest(true, accountId, transId, parameterMap,url);
        // 如果此处响应失败即为获取token失败，返回失败原因
        if (!tokenResultDTO.getCode().equals(ErrorCode.SUCCESS.getCode())) {
            ErrorCode errorCode = ErrorCode.getByCode(tokenResultDTO.getCode());
            log.warn("[{}] query simCommunicationFunctionBatch token error first,error message is {}.",transId, errorCode.getMsg());
            responseData.setCode(Integer.valueOf(errorCode.getCode()));
            responseData.setMessage(errorCode.getMsg());
        }
        // 获取token成功后解析api接口的响应结果
        String responseStr = tokenResultDTO.getMsg();
        String code = CommonUtil.getApiRspCode(responseStr);
        // 获取api接口的响应结果状态码，如果状态码为12021（token不存在或已过期），则重新获取token后再次调用api接口
        if (ErrorCode.ERR12021.getCode().equals(code)) {
            log.warn("[{}] token[accountId:{}] is invalid and start to get a new token.", transId, accountId);
            tokenResultDTO = doRequest(false, accountId, transId,  parameterMap,url);
            // 如果此处响应失败即为获取token失败，返回失败原因
            if (!tokenResultDTO.getCode().equals(ErrorCode.SUCCESS.getCode())) {
                ErrorCode errorCode = ErrorCode.getByCode(tokenResultDTO.getCode());
                log.warn("[{}] query simCommunicationFunctionBatch token error second,error message is {}.",transId, errorCode.getMsg());
                responseData.setCode(Integer.valueOf(errorCode.getCode()));
                responseData.setMessage(errorCode.getMsg());
            }
            // 获取token成功后，重置responseStr（api接口的响应结果）
            responseStr = tokenResultDTO.getMsg();
        }
        log.info("[{}] query result string is {}.", transId, responseStr);
        // 解析api响应结果
        ResponseDto<Map<String, String>> responseDto = JsonUtil.parseJson(responseStr,
                new TypeReference<ResponseDto<Map<String, String>>>(){});
        code = responseDto.getStatus();
        if (ErrorCode.SUCCESS.getCode().equals(code)) {
            String responseJobId = responseDto.getResult().get(0).get(Constants.RESPONSE_JOB_ID_1);
            BusinessJobIdDTO businessJobIdDTO = new BusinessJobIdDTO();
            businessJobIdDTO.setJobId(responseJobId);
            return ResponseData.success(businessJobIdDTO);
        } else {
            // 如果api接口返回失败，则返回失败原因
            log.warn("[{}] reset simCommunicationFunctionBatch error, error message is {}.", transId, responseDto.getMessage());
            responseData = new ResponseData();
            responseData.setCode(Integer.valueOf(code));
            responseData.setMessage(responseDto.getMessage());
            return responseData;
        }
    }

    /**
     * APN功能开停
     *
     * @param transId
     * @param accountId 集团id
     * @param cardType 卡号类型（msisdn、iccid、imsi一种）
     * @param cardNum 卡号（与类型对应）
     * @param apnName apn名称
     * @param operType 操作类型 0-开，1-停
     * @return 返回 response data
     * @throws IOException
     * @author 何光鑫
     * @date 20200326 19:12:20
     * @modify 20200326 何光鑫 v1.1.2.1 创建
     * @since v1.1.2.1
     */
    public ResponseData operateApnFunction(String transId, String accountId, String cardType, String cardNum,String apnName,String operType) throws IOException {
        ResponseData responseData = new ResponseData();
        // 封装调用api接口的参数
        Map<String, String> parameterMap = new HashMap<>();
        String url = UrlConstants.OPERATE_APN_FUNCTION;
        parameterMap.put(cardType, cardNum);
        parameterMap.put(Constants.REQUEST_PARAM_APNNAME, apnName);
        parameterMap.put(Constants.REQUEST_PARAM_OPERTYPE,operType);
        // 调用api接口：先获取token，然后带着token调用api接口
        ResultDTO tokenResultDTO = doRequest(true, accountId, transId, parameterMap,url);
        // 如果此处响应失败即为获取token失败，返回失败原因
        if (!tokenResultDTO.getCode().equals(ErrorCode.SUCCESS.getCode())) {
            ErrorCode errorCode = ErrorCode.getByCode(tokenResultDTO.getCode());
            log.warn("[{}] operateApnFunction token error first,error message is {}.",transId, errorCode.getMsg());
            responseData.setCode(Integer.valueOf(errorCode.getCode()));
            responseData.setMessage(errorCode.getMsg());
        }
        // 获取token成功后解析api接口的响应结果
        String responseStr = tokenResultDTO.getMsg();
        String code = CommonUtil.getApiRspCode(responseStr);
        // 获取api接口的响应结果状态码，如果状态码为12021（token不存在或已过期），则重新获取token后再次调用api接口
        if (ErrorCode.ERR12021.getCode().equals(code)) {
            log.warn("[{}] token[accountId:{}] of operateApnFunction is invalid and start to get a new token.", transId, accountId);
            tokenResultDTO = doRequest(false, accountId, transId,  parameterMap,url);
            // 如果此处响应失败即为获取token失败，返回失败原因
            if (!tokenResultDTO.getCode().equals(ErrorCode.SUCCESS.getCode())) {
                ErrorCode errorCode = ErrorCode.getByCode(tokenResultDTO.getCode());
                log.warn("[{}] operateApnFunction token error second,error message is {}.",transId, errorCode.getMsg());
                responseData.setCode(Integer.valueOf(errorCode.getCode()));
                responseData.setMessage(errorCode.getMsg());
            }
            // 获取token成功后，重置responseStr（api接口的响应结果）
            responseStr = tokenResultDTO.getMsg();
        }
        log.info("[{}] operate ApnFunction result string is {}.", transId, responseStr);
        // 解析api响应结果
        ResponseDto<Map<String, String>> responseDto = JsonUtil.parseJson(responseStr,
                new TypeReference<ResponseDto<Map<String, String>>>(){});
        code = responseDto.getStatus();
        if (ErrorCode.SUCCESS.getCode().equals(code)) {
            return ResponseData.success(JsonUtil.toJson(responseDto.getResult()));
        } else {
            // 如果api接口返回失败，则返回失败原因
            log.warn("[{}] operate ApnFunction error, error message is {}.", transId, responseDto.getMessage());
            responseData = new ResponseData();
            responseData.setCode(Integer.valueOf(code));
            responseData.setMessage(responseDto.getMessage());
            return responseData;
        }
    }

    /**
     * 单卡短信功能开停
     *
     * @param transId
     * @param accountId 集团id
     * @param cardType 卡号类型（msisdn、iccid、imsi一种）
     * @param cardNum 卡号（与类型对应）
     * @param operType 操作类型 0-开，1-停
     * @return 返回 response data
     * @throws IOException
     * @author 何光鑫
     * @date 20200325 16:00:24
     * @modify 20200325 何光鑫 v1.1.2.1 创建
     * @since v1.1.2.1
     */
    public ResponseData simSmsFunction(String transId, String accountId, String cardType, String cardNum, String operType) throws IOException {
        ResponseData responseData = new ResponseData();
        // 封装调用api接口的参数
        Map<String, String> parameterMap = new HashMap<>();
        String url = UrlConstants.SIM_SMS_FUNCTION;
        parameterMap.put(cardType,cardNum);
        parameterMap.put(Constants.REQUEST_PARAM_OPERTYPE,operType);
        // 调用api接口：先获取token，然后带着token调用api接口
        ResultDTO tokenResultDTO = doRequest(true, accountId, transId, parameterMap,url);
        // 如果此处响应失败即为获取token失败，返回失败原因
        if (!tokenResultDTO.getCode().equals(ErrorCode.SUCCESS.getCode())) {
            ErrorCode errorCode = ErrorCode.getByCode(tokenResultDTO.getCode());
            log.warn("[{}] query simSmsFunction token error first,error message is {}.",transId, errorCode.getMsg());
            responseData.setCode(Integer.valueOf(errorCode.getCode()));
            responseData.setMessage(errorCode.getMsg());
        }
        // 获取token成功后解析api接口的响应结果
        String responseStr = tokenResultDTO.getMsg();
        String code = CommonUtil.getApiRspCode(responseStr);
        // 获取api接口的响应结果状态码，如果状态码为12021（token不存在或已过期），则重新获取token后再次调用api接口
        if (ErrorCode.ERR12021.getCode().equals(code)) {
            log.warn("[{}] token[accountId:{}] is invalid and start to get a new token.", transId, accountId);
            tokenResultDTO = doRequest(false, accountId, transId,  parameterMap,url);
            // 如果此处响应失败即为获取token失败，返回失败原因
            if (!tokenResultDTO.getCode().equals(ErrorCode.SUCCESS.getCode())) {
                ErrorCode errorCode = ErrorCode.getByCode(tokenResultDTO.getCode());
                log.warn("[{}] query simSmsFunction token error second,error message is {}.",transId, errorCode.getMsg());
                responseData.setCode(Integer.valueOf(errorCode.getCode()));
                responseData.setMessage(errorCode.getMsg());
            }
            // 获取token成功后，重置responseStr（api接口的响应结果）
            responseStr = tokenResultDTO.getMsg();
        }
        log.info("[{}] query result string is {}.", transId, responseStr);
        // 解析api响应结果
        ResponseDto<Map<String, String>> responseDto = JsonUtil.parseJson(responseStr,
                new TypeReference<ResponseDto<Map<String, String>>>(){});
        code = responseDto.getStatus();
        if (ErrorCode.SUCCESS.getCode().equals(code)) {
            return ResponseData.success(JsonUtil.toJson(responseDto.getResult()));
        } else {
            // 如果api接口返回失败，则返回失败原因
            log.warn("[{}] reset simSmsFunction error, error message is {}.", transId, responseDto.getMessage());
            responseData = new ResponseData();
            responseData.setCode(Integer.valueOf(code));
            responseData.setMessage(responseDto.getMessage());
            return responseData;
        }
    }

    /**
     * 单卡语音功能开停
     *
     * @param transId
     * @param accountId 集团id
     * @param cardType 卡号类型（msisdn、iccid、imsi一种）
     * @param cardNum 卡号（与类型对应）
     * @param operType 操作类型 0-开，1-停
     * @return 返回 response data
     * @throws IOException
     * @author 何光鑫
     * @date 20200325 16:02:11
     * @modify 20200325 何光鑫 v1.1.2.1 创建
     * @since v1.1.2.1
     */
    public ResponseData simCallFunction(String transId, String accountId, String cardType, String cardNum, String operType) throws IOException {
        ResponseData responseData = new ResponseData();
        // 封装调用api接口的参数
        Map<String, String> parameterMap = new HashMap<>();
        String url = UrlConstants.SIM_CALL_FUNCTION;
        parameterMap.put(cardType,cardNum);
        parameterMap.put(Constants.REQUEST_PARAM_OPERTYPE,operType);
        // 调用api接口：先获取token，然后带着token调用api接口
        ResultDTO tokenResultDTO = doRequest(true, accountId, transId, parameterMap,url);
        // 如果此处响应失败即为获取token失败，返回失败原因
        if (!tokenResultDTO.getCode().equals(ErrorCode.SUCCESS.getCode())) {
            ErrorCode errorCode = ErrorCode.getByCode(tokenResultDTO.getCode());
            log.warn("[{}] simCallFunction token error first,error message is {}.",transId, errorCode.getMsg());
            responseData.setCode(Integer.valueOf(errorCode.getCode()));
            responseData.setMessage(errorCode.getMsg());
        }
        // 获取token成功后解析api接口的响应结果
        String responseStr = tokenResultDTO.getMsg();
        String code = CommonUtil.getApiRspCode(responseStr);
        // 获取api接口的响应结果状态码，如果状态码为12021（token不存在或已过期），则重新获取token后再次调用api接口
        if (ErrorCode.ERR12021.getCode().equals(code)) {
            log.warn("[{}] token[accountId:{}] of simCallFunction is invalid and start to get a new token.", transId, accountId);
            tokenResultDTO = doRequest(false, accountId, transId,  parameterMap,url);
            // 如果此处响应失败即为获取token失败，返回失败原因
            if (!tokenResultDTO.getCode().equals(ErrorCode.SUCCESS.getCode())) {
                ErrorCode errorCode = ErrorCode.getByCode(tokenResultDTO.getCode());
                log.warn("[{}] simCallFunction token error second,error message is {}.",transId, errorCode.getMsg());
                responseData.setCode(Integer.valueOf(errorCode.getCode()));
                responseData.setMessage(errorCode.getMsg());
            }
            // 获取token成功后，重置responseStr（api接口的响应结果）
            responseStr = tokenResultDTO.getMsg();
        }
        log.info("[{}] simCallFunction result string is {}.", transId, responseStr);
        // 解析api响应结果
        ResponseDto<Map<String, String>> responseDto = JsonUtil.parseJson(responseStr,
                new TypeReference<ResponseDto<Map<String, String>>>(){});
        code = responseDto.getStatus();
        if (ErrorCode.SUCCESS.getCode().equals(code)) {
            return ResponseData.success(JsonUtil.toJson(responseDto.getResult()));
        } else {
            // 如果api接口返回失败，则返回失败原因
            log.warn("[{}] simCallFunction error, error message is {}.", transId, responseDto.getMessage());
            responseData = new ResponseData();
            responseData.setCode(Integer.valueOf(code));
            responseData.setMessage(responseDto.getMessage());
            return responseData;
        }
    }

    /**
     * 物联卡数据通信服务（PCC策略）开停批量办理
     *
     * @param transId
     * @param accountId 集团id
     * @param msisdns 卡号
     * @param operType 操作类型 0-开，1-停
     * @param apnName apn名称
     * @param pccCode pcc策略
     * @return 返回 response data
     * @throws IOException
     * @author 何光鑫
     * @date 20200325 16:14:36
     * @modify 20200325 何光鑫 v1.1.2.1 创建
     * @since v1.1.2.1
     */
    public ResponseData simGprsPccFunctionBatch(String transId, String accountId, String msisdns,String apnName, String pccCode,String operType) throws IOException {
        ResponseData responseData = new ResponseData();
        // 封装调用api接口的参数
        Map<String, String> parameterMap = new HashMap<>();
        String url = UrlConstants.SIM_GPRS_PCC_FUNCTION_BATCH;
        parameterMap.put(Constants.REQUEST_PARAM_MSISDNS,msisdns);
        parameterMap.put(Constants.REQUEST_PARAM_OPERTYPE,operType);
        parameterMap.put(Constants.REQUEST_PARAM_APNNAME,apnName);
        parameterMap.put(Constants.PCC_CODE,pccCode);
        // 调用api接口：先获取token，然后带着token调用api接口
        ResultDTO tokenResultDTO = doRequest(true, accountId, transId, parameterMap,url);
        // 如果此处响应失败即为获取token失败，返回失败原因
        if (!tokenResultDTO.getCode().equals(ErrorCode.SUCCESS.getCode())) {
            ErrorCode errorCode = ErrorCode.getByCode(tokenResultDTO.getCode());
            log.warn("[{}] simGprsPccFunctionBatch token error first,error message is {}.",transId, errorCode.getMsg());
            responseData.setCode(Integer.valueOf(errorCode.getCode()));
            responseData.setMessage(errorCode.getMsg());
        }
        // 获取token成功后解析api接口的响应结果
        String responseStr = tokenResultDTO.getMsg();
        String code = CommonUtil.getApiRspCode(responseStr);
        // 获取api接口的响应结果状态码，如果状态码为12021（token不存在或已过期），则重新获取token后再次调用api接口
        if (ErrorCode.ERR12021.getCode().equals(code)) {
            log.warn("[{}] token[accountId:{}] of simGprsPccFunctionBatch is invalid and start to get a new token.", transId, accountId);
            tokenResultDTO = doRequest(false, accountId, transId,  parameterMap,url);
            // 如果此处响应失败即为获取token失败，返回失败原因
            if (!tokenResultDTO.getCode().equals(ErrorCode.SUCCESS.getCode())) {
                ErrorCode errorCode = ErrorCode.getByCode(tokenResultDTO.getCode());
                log.warn("[{}] simGprsPccFunctionBatch token error second,error message is {}.",transId, errorCode.getMsg());
                responseData.setCode(Integer.valueOf(errorCode.getCode()));
                responseData.setMessage(errorCode.getMsg());
            }
            // 获取token成功后，重置responseStr（api接口的响应结果）
            responseStr = tokenResultDTO.getMsg();
        }
        log.info("[{}] simGprsPccFunctionBatch result string is {}.", transId, responseStr);
        // 解析api响应结果
        ResponseDto<Map<String, String>> responseDto = JsonUtil.parseJson(responseStr,
                new TypeReference<ResponseDto<Map<String, String>>>(){});
        code = responseDto.getStatus();
        if (ErrorCode.SUCCESS.getCode().equals(code)) {
            String responseJobId = responseDto.getResult().get(0).get(Constants.RESPONSE_JOB_ID);
            BusinessJobIdDTO businessJobIdDTO = new BusinessJobIdDTO();
            businessJobIdDTO.setJobId(responseJobId);
            return ResponseData.success(businessJobIdDTO);
        } else {
            // 如果api接口返回失败，则返回失败原因
            log.warn("[{}] simGprsPccFunctionBatch error, error message is {}.", transId, responseDto.getMessage());
            responseData = new ResponseData();
            responseData.setCode(Integer.valueOf(code));
            responseData.setMessage(responseDto.getMessage());
            return responseData;
        }
    }

    /**
     * request 请求
     *
     * @param tokenValid true表示使用已经初始化了的token值，false表示使用重新生成的token值
     * @param accountId 集团id
     * @param transId
     * @param parameterMap 调用api接口的参数
     * @param url 调用API接口的请求路径
     * @return 返回 API接口响应结果（或者获取token失败的原因）
     * @throws IOException
     * @author 何光鑫
     * @date 20200325 15:28:29
     * @modify 20200325 何光鑫 v1.1.2.1 创建
     * @since v1.1.2.1
     */
    private ResultDTO doRequest(boolean tokenValid, String accountId, String transId, Map<String, String> parameterMap, String url) throws IOException {
        // 根据 accountId 查询 token
        ResultDTO tokenResultDTO = tokenService.getToken(tokenValid, accountId);
        if (!tokenResultDTO.getCode().equals(ErrorCode.SUCCESS.getCode())) {
            return tokenResultDTO;
        }
        parameterMap.put(Constants.REQUEST_PARAM_TOKEN, tokenResultDTO.getMsg());
        parameterMap.put(Constants.REQUEST_PARAM_TRANSID, transId);
        String methodUrl = apiRequestUtil.buildRequestUrl(parameterMap, url);
        log.info("[{}] query generalCommunicate request url is {}.", transId, methodUrl);
        tokenResultDTO.setMsg(apiRequestUtil.doGet(methodUrl));
        return tokenResultDTO;
    }
}
